<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handleInputChange"></a-input>
    <a-button type="primary" @click="addItemTodo">添加事项</a-button>
    <a-list bordered :dataSource="infolist" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked = "item.done" @change="(e) => {cbStatusChanged(e,item.id)}">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItemById(item.id)">删除</a>
      </a-list-item>
      <!-- footer区域 -->
      <div class="footer" slot="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type=" viewKey === 'all' ? 'primary' : 'default' " @click="changList('all')">全部</a-button>
          <a-button :type=" viewKey === 'undone' ? 'primary' : 'default' " @click="changList('undone')">未完成</a-button>
          <a-button :type=" viewKey === 'done' ? 'primary' : 'default' " @click="changList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex';
export default {
  name: "todo ",
  data() {
    return {
      
    };
  },
  created() {
     this.$store.dispatch('getList')
  },
  computed:{
     ...mapState(['inputValue','viewKey']),
     //...mapState({inputValue:(state)=>state.todo.inputValue},{viewKey:(state)=>state.todo.inputValue}),
     ...mapGetters(['unDoneLength','infolist'])
  },
  methods: {
     handleInputChange(e){
        //发生变化实时拿到的值
        //console.log(e.target.value)
        this.$store.commit('setInputValue',e.target.value)
     },
     //向列表中添加事项
     addItemTodo(){
        //console.log(this.$store.state.todo.inputValue)
        if(this.inputValue.trim().length <= 0){
           return this.$message.warning('文本框内容不能为空')
        }else{
           //调用store里面mutation里面的additem实现 将收集到的InputValue赋值给 list
           this.$store.commit('addItem')
        }
     },
     //根据id删除item事项
     removeItemById(id){
        this.$store.commit('removeItem',id)
     },
     //监听复选框状态变化的事件
     cbStatusChanged(e, id){
        //console.log(e.target.checked);最新的选中状态
        const parms = {
           id:id,
           status:e.target.checked
        }
        this.$store.commit('changeStatus',parms)
     },
     //清除已完成任务
     clean(){
        this.$store.commit('cleanDone')
     },
     //changList
     changList(key){
        this.$store.commit('changeViewKey',key)
     }

     
  },
};
</script>

<style scoped>
#app {
  padding: 10px;
}
.my_ipt {
  width: 500px;
  margin-right: 10px;
}
.dt_list {
  width: 500px;
  margin-top: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>